package com.example.blackjack;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

@SuppressLint({ "DrawAllocation", "WrongCall" })
public class MainGame extends View{

	Bitmap bClear, bDeal, bHit, bStand, bDouble, bSplit; 

	//Buttons bButtons;
	Coins gCoins;
	Carts gCart;
	
	Bitmap bgr;
	private int widthMainText;
	private int heightMainText;
	
	private int nNoBetX = 0;
	private int nNoBetY = 0;

	private boolean m_bDeal = true;

	private int nButtonH;

	private int nButtonW;
	
	public MainGame(Context context) {
		super(context);
		
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		widthMainText = metrics.widthPixels;
		heightMainText = metrics.heightPixels;
	
		bClear = BitmapFactory.decodeResource(getResources(), R.drawable.btn_clear);
		bDeal = BitmapFactory.decodeResource(getResources(), R.drawable.btn_deal);
		
		bSplit = BitmapFactory.decodeResource(getResources(), R.drawable.btn_split);
		bHit = BitmapFactory.decodeResource(getResources(), R.drawable.btn_hit);
		bStand = BitmapFactory.decodeResource(getResources(), R.drawable.btn_stand);
		bDouble = BitmapFactory.decodeResource(getResources(), R.drawable.btn_double);

		if (widthMainText == 480){
			nNoBetX = 200;
			nNoBetY = 750;
			
			nButtonH = 55;
			nButtonW = 120;
			
		}
		
		// TODO Auto-generated constructor stub
		//bButtons = new Buttons(context);
		gCoins = new Coins(context);
		gCart = new Carts(context);
		
		bgr = BitmapFactory.decodeResource(getResources(), R.drawable.bgrgreen);
	}
	
	public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
	    int width = bm.getWidth();
	    int height = bm.getHeight();
	    float scaleWidth = ((float) newWidth) / width;
	    float scaleHeight = ((float) newHeight) / height;
	    // CREATE A MATRIX FOR THE MANIPULATION
	    Matrix matrix = new Matrix();
	    // RESIZE THE BIT MAP
	    matrix.postScale(scaleWidth, scaleHeight);

	    // "RECREATE" THE NEW BITMAP
	    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
	    return resizedBitmap;
	}
	
	public void DrawButtons(Canvas c){
		
		if (m_bDeal ){
			c.drawBitmap(bClear, (widthMainText/2)-bClear.getWidth(), heightMainText-bClear.getHeight(), null);
			c.drawBitmap(bDeal, widthMainText/2, heightMainText-bClear.getHeight(), null);
		}else{
			if (!gCart.GetPlayerWin()){
				c.drawBitmap(getResizedBitmap(bSplit, nButtonH, nButtonW), 0, heightMainText-nButtonH, null);
				c.drawBitmap(getResizedBitmap(bHit, nButtonH, nButtonW), nButtonW, heightMainText-nButtonH, null);
				c.drawBitmap(getResizedBitmap(bStand, nButtonH, nButtonW), nButtonW*2, heightMainText-nButtonH, null);
				c.drawBitmap(getResizedBitmap(bDouble, nButtonH, nButtonW), nButtonW*3, heightMainText-nButtonH, null);
			}
		}
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Style.STROKE);
		
		canvas.drawBitmap(bgr, 0, 0, null);
		
		
		DrawButtons(canvas);
		
		gCoins.DrawCoint(canvas);
		
		gCart.DrawCards(canvas);
		
		if (gCoins.GetBet() == 0){
			paint.setColor(Color.RED);
			paint.setTextSize(28);
			
			canvas.drawText("NO BET", nNoBetX, nNoBetY, paint);
		}
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		int action = event.getAction();


		float x = event.getX();  // or getRawX();
		float y = event.getY();
		
		if (action == MotionEvent.ACTION_DOWN){
			if (gCoins.GetBet() != 0 && !gCart.GetYouLost()){
				ButtonClick(x, y);
			}else if (!m_bDeal){
				m_bDeal = true;
				gCart.ClearAll();
				gCoins.ClearCoint();
			}
			if (m_bDeal ){
				gCoins.ClickCoin(x, y);
			}
		}		
	
		invalidate();
		return true;
	}
	
	public void ButtonClick(float nX, float nY){

		if (!gCart.GetPlayerWin()){
			//Button Hit
			if (nX >= nButtonW && nX < nButtonW*2
					&& nY >= heightMainText-nButtonW && nY < heightMainText) {
				if (!m_bDeal){
					gCart.GetHitCarts();
				}
			}
			//Button stand
			if (nX >= nButtonW*2 && nX < nButtonW*3
					&& nY >= heightMainText-nButtonW && nY < heightMainText) {
				if (!m_bDeal){
					gCart.GetStandScenario();
				}
			}
		}
		
		
		
		//Button Deal
		if (nX >= (widthMainText/2) && nX < widthMainText
				&& nY >= heightMainText-bDeal.getHeight() && nY < (heightMainText-bDeal.getHeight() + bDeal.getHeight())) {
			if (m_bDeal){
				gCart.GetFirstCart();
				m_bDeal = false;
			}
			
		}
		//Button Clear
		if (nX >= (widthMainText/2)-bClear.getWidth() && nX < widthMainText/2
				&& nY >= heightMainText-bClear.getHeight() && nY < (heightMainText-bClear.getHeight() + bClear.getHeight())) {
			if (m_bDeal ){
				gCoins.ClearCoint();
			}
			
		}
	}

}
